#!/usr/bin/perl -w
#  
#
# 
use warnings;
use strict;
#
sub Positions;
# Define the DNA sequence 
#our $sequence = "CGT";
our $sequence = "CGTACTAGTCATGGGTATGCTAGCTAGTCTTTACCTATTGGACCTATGGAAAGCCATGAGTGGAATTC";
print "The DNA sequence is $sequence.\n ";
# Define the list of sequences
our @TBFSArray = ("CGT","Y","CGCTA","AGCTAGTC","CGTA","ACCTGATTTTAGAA","GGAAAGCCA");
#our @TBFSArray = ("GT");
#our @TBFSArray = ("Y");
print "The ".($#TBFSArray+1)." TBFS are \n";
foreach ( my $ i =0; $i <$#TBFSArray+1; $i++){
	print $TBFSArray[$i]."\n";
}  

my  @test = @{Positions($sequence,\@TBFSArray)};
 #print "test".$test[0];
print "length test".$#test."\n";
foreach ( my $ i =0; $i <$#TBFSArray+1; $i++){
    print "######".$test[$i]."\n";
}  
#


sub Positions{
    # Get the sequence that was passed as a reference 
    my @seq = split(//, shift(@_));
    # Get the array of TFBSs
    my $refTFBS = shift(@_);
    # Dereference the array
    my @array = @{$refTFBS}; 
    # Get the number of elements in the DNA sequence
    my $numSeq  = $#seq; 
    # Get the number of TFBSs
    my $numTFBS = $#array;
    
    # Declare the array
    my @returned = (); 
    for (my $index1 =0; $index1<= $numTFBS; $index1++){
        my @TFBS = split(//,$array[$index1]);
        my $numNuc =  $#TFBS;
        my $found = 0;
        print("TFBS ".$index1." ".$array[$index1]."\n");
#        print($index1 ."   ". $numNuc."\n");
#        my $diff = $numSeq-$index1; 
#        print($diff."\ ".$numNuc."\n");
        $found = 1;
        # loop through the sequence
       	my $index2 = 0;
        while ($index2<=$numSeq-$numNuc){
        	$found =1;
       # for (my $index2 = 0; $index2<= $numSeq; $index2++){
        	# loop through the TFBS
        	my $index3 = 0;
        	while (($index3<= $numNuc)){
            #for (my $index3 = 0; $index3<= $numNuc; $index3++){
               print("---index 2 and 3: ".$index2."\ ".$index3."\n");
               print( "Sequence index ".($index2+$index3)." and TFBS index  ".$index3."\n");
              #   if ($index2+$index3 < $numSeq){
                    print( "sequence and TFBS nucleotide:".$seq[$index2+$index3]." ".$TFBS[$index3]."\n");
   	                if($seq[$index2+$index3] ne $TFBS[$index3]){
   	        	        $found = 0;
   	        	     
                		
   	                } 
               # }
                $index3++;
            }
            # break the loop over DNA sequence if the TFBS is found.
            print("Found is $found\n");
            last if ($found);
            $index2++;   
        } 
        
           
      push(@returned,$found); 
    } 
    return \@returned;
}
